<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>分类管理 - 后台管理</title>
    <div th:replace="~{back/header.html}"></div>
</head>
<body>
<div id="wrapper">
    <!-- 引入导航栏 -->
    <nav th:replace="~{back/header.html}"></nav>

    <div id="page-wrapper">
        <div class="container-fluid">
            <!-- Page Heading -->
            <div class="row">
                <div class="col-lg-12">
                    <h1 class="page-header">分类管理</h1>
                    <ol class="breadcrumb">
                        <li><i class="fa fa-dashboard"></i> <a th:href="@{/admin}">仪表盘</a></li>
                        <li class="active"><i class="fa fa-tags"></i> 分类列表</li>
                    </ol>
                </div>
            </div>
            <!-- /.row -->

            <div class="row">
                <div class="col-lg-12">
                    <p>
                        <a th:href="@{/admin/category/edit}" class="btn btn-primary">新增分类</a>
                    </p>
                    <div class="table-responsive">
                        <table class="table table-bordered table-hover table-striped">
                            <thead>
                            <tr>
                                <th style="width: 30%; min-width: 100px;">ID</th>
                                <th style="width: 40%; min-width: 180px;">分类名称</th>
                                <th style="width: 10%;">文章数量</th>
                                <th style="width: 20%;">操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <tr th:each="category : ${categories.list}">
                                <td th:text="${category.id}">1</td>
                                <td th:text="${category.name}">默认分类</td>
                                <td th:text="${category.count}">0</td>
                                <td>
                                    <a th:href="@{'/admin/category/edit?id=' + ${category.id}}" class="btn btn-xs btn-info">编辑</a>
                                    <a href="javascript:void(0);" th:onclick="|confirmDelete(${category.id})|" class="btn btn-xs btn-danger">删除</a>
                                </td>
                            </tr>
                            <tr th:if="${categories.list.size() == 0}">
                                <td colspan="4" class="text-center">暂无分类</td>
                            </tr>
                            </tbody>
                        </table>
                    </div>

                    <!-- 分页 -->

                </div>
            </div>
            <!-- /.row -->
        </div>
        <!-- /.container-fluid -->
    </div>
    <!-- /#page-wrapper -->
</div>
<!-- /#wrapper -->

<div th:replace="~{back/footer.html}"></div>

<script type="text/javascript">
    function confirmDelete(categoryId) {
        if (confirm("确定要删除这个分类吗？")) {
            $.ajax({
                url: '/admin/category/delete',
                type: 'POST',
                data: {id: categoryId},
                success: function(response) {
                    if (response.code === 1) {
                        alert('删除成功！');
                        window.location.reload();
                    } else {
                        alert('删除失败：' + response.msg);
                    }
                },
                error: function() {
                    alert('请求失败，请稍后重试。');
                }
            });
        }
    }
</script>

</body>
</html>